package com.lsfb.duishangbaos.Login;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

import com.lsfb.duishangbaos.HomePage.HomePageActivity;
import com.lsfb.duishangbaos.Login.ShopEnter.ShopEnterActivity;
import com.lsfb.duishangbaos.R;
import com.lsfb.duishangbaos.Utils.LittleUtils;
import com.lsfb.duishangbaos.Utils.NoDoubleClickUtils;
import com.lsfb.duishangbaos.Utils.URLString;
import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.LsfbActivity;
import com.zgscwjm.lsfbbasetemplate.annotation.ContentView;
import com.zgscwjm.lsfbbasetemplate.annotation.OnClick;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.config.Config;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;
import com.zgscwjm.lsfbbasetemplate.net.MD5;
import com.zgscwjm.lsfbbasetemplate.utils.SN;
import com.zgscwjm.lsfbbasetemplate.utils.SPUtils;
import com.zgscwjm.lsfbbasetemplate.utils.T;

import java.util.HashMap;


/**
 * Created by Administrator on 2017/5/9 0009.
 */
@ContentView(R.layout.aty_login)
public class LoginActivity extends LsfbActivity {

    /**
     * 账号
     */
    @ViewInject(R.id.aty_login_et_phone)
    private EditText et_phone;

    /**
     * 密码
     */
    @ViewInject(R.id.aty_login_et_pwd)
    private EditText et_pwd;

    @ViewInject(R.id.aty_login_btn_login)
    private Button btn_login;

    @Override
    public void initView() {
        et_phone.setText((String) SPUtils.get(this, "et_phone", ""));
        et_pwd.setText((String) SPUtils.get(this, "et_pwd", ""));

        String userid = (String) SPUtils.get(getApplicationContext(), "userid1", "-1");

        if (!userid.equals("-1")) {
            btn_login.setClickable(false);
            T.showShort(this, "自动登录中..");
            login();
        }
    }

    @Override
    public void initEvent() {

    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.aty_login_btn_login, R.id.aty_login_btn_add})
    public void onClick(View view) {
        switch (view.getId()) {
            //登录
            case R.id.aty_login_btn_login:
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(et_phone.getWindowToken(), 0);
                imm.hideSoftInputFromWindow(et_pwd.getWindowToken(), 0);
//                startActivity(new Intent(this, HomePageActivity.class));
                if (!NoDoubleClickUtils.isDoubleClick()) {
                    login();
                }
                break;
            //商户入驻
            case R.id.aty_login_btn_add:
                startActivity(new Intent(this, ShopEnterActivity.class));
                break;
        }
    }

    @Override
    public void initData() {

    }

    public void login() {
        if (et_phone.getText().toString().isEmpty()) {
            SN.SnackShow(et_phone, "请输入账号");
            return;
        }
        if (et_pwd.getText().toString().isEmpty()) {
            SN.SnackShow(et_phone, "请输入密码");
            return;
        }

        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("account", et_phone.getText().toString());
        hashMap.put("pwd", MD5.GetMD5Code(et_pwd.getText().toString()));
        hashMap.put("rid", "123");
        new BaseInternet().getData(URLString.LOGIN, hashMap, LoginBean.class, new LoginEvent<LoginBean>(), false);
    }

    @EventAnnotation
    public void LoginEvent(LoginEvent<LoginBean> event) {
        btn_login.setClickable(true);
        switch (event.getCode()) {
            case 200:
                T.showShort(this, event.getMsg());
                LittleUtils.saveuser(event.getData().getUserid(), getApplicationContext());
                Config.setUserId(event.getData().getUserid());
                SPUtils.put(this, "et_phone", et_phone.getText().toString());
                SPUtils.put(this, "et_pwd", et_pwd.getText().toString());
                startActivity(new Intent(this, HomePageActivity.class));
                break;
            default:
                SN.SnackShow(et_phone, event.getMsg());
                break;
        }
    }


    //点击退出弹出确认提示框
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            dialog();
            return true;
        }
        return true;
    }

    protected void dialog() {

//    通过AlertDialog.Builder这个类来实例化我们的一个AlertDialog的对象
        AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.MyAlertDialogStyle);
//    设置Title的图标
        builder.setIcon(R.mipmap.ic_launcher);
//    设置Title的内容
        builder.setTitle("温馨提示");
//    设置Content来显示一个信息
        builder.setMessage("确定要退出应用吗？");
//    设置一个PositiveButton
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                android.os.Process.killProcess(android.os.Process.myPid());
            }
        });
//    设置一个NegativeButton
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
//    显示出该对话框
        builder.show();
    }


}
